﻿using Hhmocon.Mes.Application;
using Hhmocon.Mes.Application.Base;
using Hhmocon.Mes.Application.Energy;
using Hhmocon.Mes.Repository.Domain;
using Hhmocon.Mes.Repository.Domain.Energy;
using Hhmocon.Mes.Util;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Hhmocon.Mes.WebApi.Controllers.Energy
{
    [Route("api/[controller]/[action]")]
    [EnableCors(PolicyName = "_hhmoconmesAllowSpecificOrigins")]
    [ApiController]

    public class EnergyConsumptionController
    {
        private EnergyConsumptionApp _app;
        private PikachuApp _pikachuApp;

        public EnergyConsumptionController(EnergyConsumptionApp app, PikachuApp picachuRepository)
        {
            _app = app;
            _pikachuApp = picachuRepository;
        }


        [HttpPost]
        public Response<PageData> GetList(PageReq req)
        {
            var result = new Response<PageData>();
            try
            {
                PageData pd = new PageData();
                string a = "2020-01";
                string c = a.Split("-")[0];
                string b = a.Split("-")[1];

                long lcount = 0;
                pd.Data = _pikachuApp.GetList<energy_consumption>(req, ref lcount);
                pd.Total = lcount;
                result.Result = pd;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 总能耗统计界面接口
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<List<energy_consumption_statistics>> GetStatistics(DateTime date,bool pieChart)
        {
            var result = new Response<List<energy_consumption_statistics>>();
            try
            {            
                if (_app.getStatistics(date, pieChart) ==null)
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }

                result.Result = _app.getStatistics(date,pieChart);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 获取同比环比数据 catagory 为
        /// power_consumption 
        /// natural_gas_energy_consumption
        /// coalr_consumption
        /// oil_energy_consumption
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<List<CompareData>> GetCompareData(DateTime date, string category)
        {
            var result = new Response<List<CompareData>>();
            try
            {
                if (_app.getCompareData(date, category) == null)
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }

                result.Result = _app.getCompareData(date,category);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 获取消耗统计数据，category分别为 power_consumption，natural_gas_energy_consumption，coalr_consumption，oil_energy_consumption
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<ReturnData> GetConsumptionStatistics(DateTime startTime, DateTime endTime, string category)
        {
            var result = new Response<ReturnData> ();
            try
            {
                if (startTime == "0001/1/1 0:00:00".ToDate() || endTime == "0001/1/1 0:00:00".ToDate())
                {
                    endTime = DateTime.Now;
                    startTime = DateTime.Now.AddMonths(-6);
                }

                if (_app.getConsumptionStatistics(startTime, endTime, category) == null)
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }

                result.Result = _app.getConsumptionStatistics(startTime, endTime, category);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 获取费用统计，category为power_expense，natural_gas_energy_expense，coalr_expense，oil_energy_expense
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<ReturnData> GetExpensstatistics(DateTime startTime, DateTime endTime, string category)
        {
            var result = new Response<ReturnData>();
            try
            {
                if (startTime == "0001/1/1 0:00:00".ToDate()|| endTime== "0001/1/1 0:00:00".ToDate())
                {
                    endTime = DateTime.Now;
                    startTime = DateTime.Now.AddMonths(-6);
                }
                if (_app.getExpensstatistics(startTime, endTime, category) == null)
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }

                result.Result = _app.getExpensstatistics(startTime, endTime, category);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据设ID删除数据,可批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string[]> Delete(string[] ids)
        {
            var result = new Response<string[]>();
            try
            {
                result.Result = ids;

                if (!_pikachuApp.Delete_Mask<energy_consumption>(ids))
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 新建数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Create(energy_consumption obj)
        {
            var result = new Response<string>();
            try
            {
                energy_consumption data = _app.Insert(obj);
                if (data != null)
                {
                    result.Result = data.energy_consumption_id;
                }
                else
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "数据写入失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        [HttpPost]
        public Response<List<pieData>> GetPieData(DateTime date)
        {
            var result = new Response<List<pieData>>();
            try
            {
                if (_app.getPieData(date) == null)
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }

                result.Result = _app.getPieData(date);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 费用柱状图
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<List<GramData>> getExpenseData(DateTime date)
        {
            var result = new Response<List<GramData>>();
            try
            {
                if (_app.getExpenseData(date) == null)
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }

                result.Result = _app.getExpenseData(date);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 能耗柱状图
        /// </summary>
        /// <param name="date"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<List<GramData>> getConsumptionData(DateTime date)
        {
            var result = new Response<List<GramData>>();
            try
            {
                if (_app.getConsumptionData(date) == null)
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }

                result.Result = _app.getConsumptionData(date);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }
}
